$(function () {
    $('#oldPassword').blur(validatePass)
    $('#newPassword').blur(validateNewPass)
    $('#reNewPassword').blur(validateReNewPass)
    //表单提交
    $('.updatePassForm').submit(function (e) {
      e.preventDefault()
      let f1 = validatePass()
      let f2 = validateNewPass()
      let f3 = validateReNewPass()
      let flag = f1 & f2 & f3
      if (flag === 0) {
        return alert('请正确填写密码信息')
      }
      updatePass()
    })
  })
  
  /**
   * 修改密码的函数
   * @date 2021-05-11
   * @returns {any}
   */
  async function updatePass() {
    let options = {
      url: 'http://146.56.230.35:3007/users/updatePassword',
      type: 'PUT',
      data: {
        id: getUserId(),
        userPassword: $('#newPassword').val().trim(),
      },
    }
    let res = await request(options)
    console.log(res)
    if (res.code !== 200) {
      return alert(res.msg)
    }
    
    sessionStorage.clear()
    location = 'login.html'
  }
  
  //验证原始密码
  function validatePass() {
    let oldPassword = $('#oldPassword')
    let v = oldPassword.val()
    let div = oldPassword.parent()
    let span = oldPassword.next()
    div[0].className = ''
    span.html('')
    let str = new MD5().hex_md5(v)
    let pass = getUserPassword()
    if (str !== pass) {
      div[0].className = 'error'
      span.html('旧密码不正确')
      return false
    }
    div[0].className = 'ok'
    span.html('旧密码正确')
    return true
  }
  
  //验证新密码
  function validateNewPass() {
    let newPassword = $('#newPassword')
    let v = newPassword.val()
    let div = newPassword.parent()
    let span = newPassword.next()
    div[0].className = ''
    span.html('')
    //1、判断空
    if (!v) {
      div[0].className = 'error'
      span.html('密码不能空')
      return false
    }
    //2、不能是旧密码
    if (v === $('#oldPassword').val()) {
      div[0].className = 'error'
      span.html('新密码不能和旧密码一致')
      return false
    }
    //3、判断符合规则
    let flag = false
    let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_$(){%},;'
    for (let i = 0; i < v.length; i++) {
      if (str.indexOf(v[i]) >= 0) {
        flag = true
        break
      }
    }
    if (!flag) {
      div[0].className = 'error'
      span.html('密码必须包含大写字符或特殊字符')
      return false
    }
    //4、判断长度6-20
    if (v.length < 6 || v.length > 20) {
      div[0].className = 'error'
      span.html('密码长度6-20位')
      return false
    }
    div[0].className = 'ok'
    span.html('密码正确')
    return true
  }
  
  //验证二次输入密码
  function validateReNewPass() {
    let reNewPassword = $('#reNewPassword')
    let v = reNewPassword.val()
    let div = reNewPassword.parent()
    let span = reNewPassword.next()
    div[0].className = ''
    span.html('')
    //新密码是否正确
    if (!validateNewPass()) {
      div[0].className = 'error'
      span.html('请输入正确的新密码')
      return false
    }
    //两次密码是否一致
    if (v !== $('#newPassword').val()) {
      div[0].className = 'error'
      span.html('两次密码输入不一致')
      return false
    }
    div[0].className = 'ok'
    span.html('密码输入正确')
    return true
  }
  